#include "Day.h"


	/* Default Constructor */
	Day::Day()
	{
		this->meetings = new list<Meeting *>();
	}

	/* Destructor */
	Day::~Day()
	{
		if(!this->meetings->empty())
		{
			std::list<Meeting *>::iterator it;
			for ( it=this->meetings->begin() ; it != this->meetings->end(); it++ ){
				(*it)->~Meeting();
			}
		}
		delete(this->meetings);
	}

	//Copy Constructor
	Day::Day(const Day& d)
	{
		this->day = d.day;
		this->meetings = d.meetings;
	}

	/* Constructor for regular day */
	Day::Day(const int day)
	{
		Day();
		this->day = day;
	}
	//Operator '=' overloading
	const Day& Day::operator=(const Day& d){
		if (this!=&d) { 
			this->assign(d);
		}
		return *this;
	}


	int Day::getDay() const
	{
		return this->day;
	}

	//Adds a specific meeting to the day
	const bool Day::addMeeting( Meeting &meeting)
	{
		E_Meeting* e = (E_Meeting*)&meeting;
		if(!this->findOverlap(meeting))
		{
			if (meeting.getWhoAmI()==BASE)
			{
				this->meetings->push_back(new Meeting(meeting));
			}
			else
			{
				E_Meeting* meet_to_add = new E_Meeting(*e);
				this->meetings->push_back(meet_to_add);
			}
				cout << "Meeting added" << endl;
				return true;
		}
		cout << "Can't add meeting, overlap exists" << endl;
		return false;
	}
	/*
	@retrun: true iff succeeded delete meeting else false
	*/
	const bool Day::delMeeting(const Meeting &meeting)
	{
		Meeting* meet_to_del = findMeeting(meeting.getStartHour());
		if(meet_to_del == NULL)
		{
			cout << "Can't delete this meeting" << endl;
			return false;
		}
		this->meetings->remove(meet_to_del);
		cout << "Meeting deleted" << endl;
		return true;
	}

	void Day::printMeetings() const
	{
		if(!this->meetings->empty())
		{
			std::list<Meeting *>::iterator it;
			char c = 'A';
			for ( it=this->meetings->begin() ; it != this->meetings->end(); it++ ){
				cout << "*** Meeting: " << c++ << " ***" <<endl;
				(*it)->printMeeting();
			}
		}
		else
		{
			cout << "No meetings today!" << endl;
		}
	}

	/*
	@pre meeting != all meetings in this->meetings
	@return value: if there is overlap retVal = true else retVal = false
	*/
	const bool Day::findOverlap(const Meeting &meeting) const
	{
		std::list<Meeting *>::const_iterator it;
		for(it = this->meetings->begin() ; it != this->meetings->end() ; it++)
		{
			//Calls the right virtual function needed to determine whther there is an overlap
			if(meeting.overlapWith(**it))
				return true;
		}
		return false;
	}

	Meeting* Day::findMeeting(const float start_t) const
	{
		std::list<Meeting *>::iterator it;
		Meeting* ret_meeting = NULL;
		for(it = this->meetings->begin() ; it != this->meetings->end() ; it++)
		{
			if((*it)->getStartHour() == start_t)
			{
				ret_meeting = *it;
				break;
			}
		}
		if(ret_meeting == NULL)
			cout << "Meeting not found" << endl;

		return ret_meeting;
	}


//Used for the Operator '=' overloading
	const Day& Day::assign(const Day& d){
	std::list<Meeting *>::const_iterator it;
	std::list <Meeting*> *new_meetings = new std::list<Meeting*>;
	for(it = d.meetings->begin() ; it != d.meetings->end() ; it++)
	{
		new_meetings->push_back(*it);
	}
	delete(this->meetings);
	this->meetings = new_meetings;
	return *this;
}